//1.引入fsa原模块
const fs = require('fs');

//2.原来做法 多个异步任务会往前缩进，会产生回调地狱问题
/* fs.readFile('one.md', (err, data1) => {
    fs.readFile('two.md', (err, data2) => {
        fs.readFile('three.md', (err, data3) => {
            if(err) throw err;
            let result = data1 + '\r\n' + data2 + '\r\n' + data3
            console.log(result);
        })
    })
}) */

//3.Promise实现 多个异步任务代码不会往前缩进且不会产生回到地狱的问题
/* const p = new Promise((resolve, reject) =>{
    fs.readFile('one.md', (err, data) => {
       resolve(data);
    });
});
//调用then方法
p.then(value => {
    return new Promise((resolve, reject) => {
        fs.readFile('two.md', (err, data) => {
            resolve([value, data]);
         });
    });
}).then(value =>{
    return new Promise((resolve, reject) => {
        fs.readFile('three.md', (err, data) => {
            value.push(data);
            resolve(value);
         });
    })
}).then(value => {
    console.log(value.join('\r\n'));
})

 */
//4.链式链式调用第二种写法
/* const p = new Promise((resolve, reject) => {
    fs.readFile('one.md', (err, data) => {
        if (err) throw err;
        resolve(data);
    })
})

//调用then方法
p.then(value => {
    return new Promise((resolve, reject) => {
        fs.readFile('two.md', (err, data) => {
            resolve([value, data]);
        });
    }).then(value => {
        return new Promise((resolve, reject) => {
            fs.readFile('three.md', (err, data) => {
                value.push(data);
                resolve(value);
            });
        }).then(value => {
            console.log(value.join('\r\n'));
        })
    })
}) */

//5.读书one和two两个文件
const p = new Promise((resolve, reject) => {
    fs.readFile('one.md', (err, data) => {
        if(err) throw err;
        resolve(data);
    })
})

p.then(value => {
    return new Promise((resolve, reject) => {
        fs.readFile('two.md', (err, data) => {
            resolve([value, data]);
        })
    }).then(value => {
        console.log(value.join('\r\n'));
    })
})